<?php
if (!defined('IN_IA')) {
	die('Access Denied');
}
global $_W, $_GPC;
$id = $_GPC['experienceid'];
$openid = m('user')->getOpenid();
$member = m('member')->getInfo($openid);
$time = time();
$op = empty($_GPC['op']) ? 'display' : $_GPC['op'];
if($op == 'display'){
        $experience = pdo_fetch("SELECT *,endtime as overtime  FROM " . tablename('ewei_shop_commission_experience') . " WHERE id = '{$id}' and uniacid = '{$_W['uniacid']}'");
        if($experience){
           $experience['starttime']=date('Y.m.d H:i',$experience['starttime']); 
           $experience['endtime']=date('Y.m.d H:i',$experience['overtime']);
        }
        $access_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx4d6a0644cae71c05&secret=e57902975056e739bae60ae12c6c1419";
        $access_msg = json_decode(file_get_contents($access_token));
        $token = $access_msg->access_token;
        $subscribe_msg = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$token&openid=$_W[openid]";
        $subscribe = json_decode(file_get_contents($subscribe_msg));
        $gzxx = $subscribe->subscribe;
            $userinfo= pdo_fetch("SELECT * FROM " . tablename('ewei_shop_member') . " WHERE uid = '{$member['uid']}' and uniacid = '{$_W['uniacid']}'");
            if(empty($userinfo['realname']) || empty($userinfo['mobile']) || empty($userinfo['weixin']) || empty($userinfo['province']) || empty($userinfo['city'])){
                 if($gzxx === 1){
                 $url="http://wx.lanxuevip.com/app/index.php?i=5&c=entry&p=info&do=member&saoma=true&m=ewei_shop&exper=".$_GPC['do']."&id=".$id;
                 header("Location: $url");
                 exit;
                 }
        }
        $experience_log = pdo_fetch("SELECT * FROM " . tablename('ewei_shop_commission_experience_record') . " WHERE openid = '{$openid}' and experienceid='{$id}' and uniacid = '{$_W['uniacid']}'");
        //只能领取一次
        $experience_record_log_id=$experience_log['id'];
        
         //不需要支付的体验券
        if($experience['cash']==0){
            //判断是否是小店自己领取体验券
            if($experience['openid']!=$openid){
                //判断有没有领取过体验券
                if(empty($experience_log) && !empty($experience) && $experience['num']>$experience['receive_num'] && $experience['overtime']>$time){
                    //领取体验券
                    $ordersn = m('common')->createNO('commission_experience_record', 'ordersn', 'EX');
                    $insert=array(
                     'uniacid'=>$_W['uniacid'],
                     'mid'=>$experience['mid'],
                     'uid'=>$member['uid'],
                     'openid'=>$openid,
                     'ordersn'=>$ordersn,
                     'nickname'=>$userinfo['nickname'],
                     'experienceid'=>$id,
                     'createtime'=>time(),
                     'status'=>1
                    );
                    $receive_num=$experience['receive_num']+1;
                    pdo_insert('ewei_shop_commission_experience_record', $insert);
                    pdo_update('ewei_shop_commission_experience',array('receive_num'=>$receive_num),array('id'=>$id));
                    $experience_ok = 1;//领取体验券成功
                }else{
                    if($experience_log['status']==1){
                    $experience_ok = 4;//已经领取体验券
                    }elseif($experience['num']==$experience['receive_num']){
                    $experience_ok = 5; //已全部领取完  
                    }elseif($experience['num']>$experience['receive_num'] && $experience['overtime']<$time){
                    $experience_ok = 6; //已过有效期
                    }else{
                    $experience_ok = 0;//领取体验券失败    
                    }
                }
            }else{
             $experience_ok = 2;   
            }
        }
        //需要支付的体验券
        if($experience['cash']>0){
            //判断是否是小店自己领取体验券
            if($experience['openid']!=$openid){
                //判断有没有记录
                if(empty($experience_log) && !empty($experience) && $experience['num']!=$experience['receive_num'] && $experience['overtime']>$time){
                    //领取体验券
                    $ordersn = m('common')->createNO('commission_experience_record', 'ordersn', 'EX');
                    $insert=array(
                     'uniacid'=>$_W['uniacid'],
                     'mid'=>$experience['mid'],
                     'uid'=>$member['uid'],
                     'openid'=>$openid,
                     'ordersn'=>$ordersn,
                     'price'=>$experience['cash'],
                     'nickname'=>$userinfo['nickname'],
                     'experienceid'=>$id,
                     'createtime'=>time(),
                     'status'=>0
                    );
                    //$receive_num=$experience['receive_num']+1;
                    pdo_insert('ewei_shop_commission_experience_record', $insert);
                    $experience_record_log_id=pdo_insertid();
                    //pdo_update('ewei_shop_commission_experience',array('receive_num'=>$receive_num),array('id'=>$id));
                    $experience_ok = 3;
                }else{
                    if($experience_log['status']==1){
                    $experience_ok = 4; //已经支付     
                    }elseif($experience_log['status']==0 && !empty($experience_log) && !empty($experience) && $experience['num']>$experience['receive_num'] && $experience['overtime']>$time){
                    $experience_ok = 3; //去支付   
                    }elseif($experience_log['status']==0 && !empty($experience_log) && !empty($experience) && $experience['num']==$experience['receive_num']){
                    $experience_ok = 5; //已全部领取完
                    }elseif($experience_log['status']==0 && !empty($experience_log) && !empty($experience) && $experience['num']>$experience['receive_num'] && $experience['overtime']<$time){
                    $experience_ok = 6; //已过有效期
                    }else{
                    $experience_ok = 0; //领取失败   
                    }
                }
            }else{
             $experience_ok = 2;   
            }
        }
        include $this->template('experience/list');
}